import Vue from 'vue';
// 导入vuex
import Vuex from 'vuex';
//挂载
Vue.use(Vuex);

import { getCarList } from "@/http/index";

export default new Vuex.Store({
    state: {
        avatarImage: localStorage.getItem('avatarImage') && localStorage.getItem('avatarImage') != 'undefined' ? localStorage.getItem('avatarImage') : '/logo.png',
        //用户数据
        userinfo: localStorage.getItem('userinfo') && localStorage.getItem('userinfo') != 'undefined' ? JSON.parse(localStorage.getItem('userinfo')) : {},
        //购物车数据
        cartList: [],
        //全选按钮
        allChecked: false,
        //购物车商品总量
        cartCount: 0,

    },
    getters: {
        //商品总数量和总价
        getBadgeCount(state) {
            let allCount = 0 //总数量
            let allPrice = 0 //总价格
            state.cartList.forEach(v => {
                allCount += v.count;
                //如果已选中
                if (v.isChecked) {
                    allPrice += v.count * v.price * 100
                }
            })
            return {
                allCount,
                allPrice
            }
        },
        //返回购物车数据
        getCartData(state) {
            return state.cartList
        },
        //返回头像
        getAvatarImage(state) {
            console.log(state.avatarImage, '999999');
            return state.avatarImage
        }

    },
    mutations: {
        // 用于更新数据(用户数据)
        setUserInfo(state, user) {
            // localStorage中存储是防止刷新浏览器时数据丢失
            localStorage.setItem('userinfo', JSON.stringify(user))
            state.userinfo = user
        },
        //购物车数据
        setCartList(state, params) {
            state.cartList = params
        },
        // 删除购物车商品
        removeGoods(state, params) {
            const index = state.cartList.findIndex(v => v.id === params)
            state.cartList.splice(index, 1)
        },
        //全选
        setAllChecked(state, params) {
            state.allChecked = params
                // console.log('---', state.allChecked);
        },
        // 设置购物车数据（添加）
        setAddCartList(state, params) {
            const flag = state.cartList.some(v => {
                if (v.productId == params.productId) {
                    v.count = params.count
                    return true
                }
            })
            if (!flag) {
                state.cartList.push(params)
            }
        },
        setAvatarImage(state, params) {
            state.avatarImage = params
        }
    },

    actions: {
        //获取购物车数据
        getCartData(state) {
            getCarList().then((res) => {
                if (res == undefined) {
                    state.commit('setCartList', [])
                    return
                }

                let list = res
                list.forEach(v => {
                    v.isChecked = false
                });
                state.commit('setCartList', list)
            });
        },

        // 处理单选按钮
        changeChecked(state) {
            let checked = true
            state.getters.getCartData.forEach(item => {
                if (!item.isChecked) {
                    checked = false
                    return
                }
            })
            state.commit('setAllChecked', checked)
        },
        // 处理全选按钮
        changAllChecked(data) {
            data.state.cartList.forEach(v => {
                v.isChecked = !data.state.allChecked
            })
            data.commit('setAllChecked', !data.state.allChecked)
        },

    }

})